    ;(function(){
    //初始化画布尺寸
    var canvas = document.querySelector('#stage');
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;        
  })();
  
  //;(function(){
      //获取舞台元素
      var stage = new createjs.Stage("stage");
      document.querySelector('.gameWrap').style.width = window.innerWidth;
      document.querySelector('.gameWrap').style.height = window.innerHeight;
      var stageW = window.innerWidth;
      var stageH = window.innerHeight;
      createjs.Touch.enable(stage);
      
      //背景
      var background = new createjs.Shape();
      background.graphics.beginFill('#78abbe').drawRect(0,0,stageW,stageH);
      stage.addChild(background);
      var tiangong = new createjs.Bitmap("img/gamebg.jpg");
      tiangong.image.onload = function(){
      	tiangong.scaleY = tiangong.scaleX = stageW/this.naturalWidth;
      };
      stage.addChild(tiangong);
      var floor = new createjs.Bitmap("img/cloud.png");
      floor.image.onload = function(){
      	floor.scaleY = floor.scaleX = stageW/this.naturalWidth;
      	floor.y = stageH-60;
      };
      
      stage.addChild(floor);
      
      //控制主角
      /***以下主角代码***/
      var heroData = {
                "images":["./img/hero.png"],
                "frames":{"height":120,"width":120,"count":8,"spacing":-1},
                animations:{
		      		action:{
		      			frames:[0,0,0,0,0,0,0,0,0,0,
		      			             1,1,1,1,1,1,1,1,1,1,
		      			             2,2,2,2,2,2,2,2,2,2,
		      			             3,3,3,3,3,3,3,3,3,3,
		      			             4,4,4,4,4,4,4,4,4,4,
		      			             5,5,5,5,5,5,5,5,5,5,
		      			             6,6,6,6,6,6,6,6,6,6,
		      			             7,7,7,7,7,7,7,7,7,7,],
		      			next:'action',
		      			speed:2
		      		}
		      	}
      };
      var heroSheet = new createjs.SpriteSheet(heroData);
      var hero = new createjs.Sprite(heroSheet);
      //主角移动事件
      hero.moveEvent = function moveEvent(e){
              var heroTween = createjs.Tween.get(hero,{override:true});
              if(e.stageX>hero.x){
                      hero.scaleX = 0-stageW/120/5;
              }else{
                      hero.scaleX = stageW/120/5; 
              }
              heroTween.to({
                      x:e.stageX-20,
                      y:e.stageY-20},
                      Math.abs(e.stageX-hero.x)+Math.abs(e.stageY-hero.y)*4);
      }
      
      hero.scaleX = hero.scaleY = stageW/120/5;
      hero.regX = 120*hero.scaleX/2;
      hero.framerate = 5;
      hero.gotoAndPlay('action');
      //hero.play();
      stage.addChild(hero);
      hero.x = stageW/2;
      hero.y = stageH-100;
      stage.on('pressmove',hero.moveEvent);
      stage.on('pressup',hero.moveEvent);
      /***以上主角代码***/
     
     /***以下是天狗代码***/
    var flyDogData = {
    	"images":["./img/flydog.png"],
    	"frames":{"height":210,"width":340,"count":6},
    	animations:{
      		action:{
      			frames:[0,0,0,0,0,0,
      			               1,1,1,1,1,1,
      			               2,2,2,2,2,2,
      			               3,3,3,3,3,3,
      			               4,4,4,4,4,4,
      			               5,5,5,5,5,5],
      			next:'action',
      			speed:2
      		}
      	}
    };
    var flyDogSheet = new createjs.SpriteSheet(flyDogData,'action');
    flyDogSheet.framerate = 0.1;
    var flyDog = new createjs.Sprite(flyDogSheet);
    flyDog.gotoAndPlay('action');
    //flyDog.play();
    flyDog.scaleX = stageW/210/8;
    flyDog.scaleY = stageW/210/6;
    
  
       
          
    flyDog.flyDogMove = function(){
        var dan = skyGirl&&skyGirl.x;
        var dan2 = hero.y;
        if(dan2<stageH/3)var toY = stageH/4;
        else var toY = hero.y-Math.random()*stageW/3-stageW/5;
        
        if(dan>this.x)flyDog.scaleX = 0-stageW/210/8;
        else flyDog.scaleX = stageW/210/8;
        createjs.Tween.get(this,{override:true}).to({
                x:dan,
                y:toY
        },(Math.abs(this.x-dan)+Math.abs(this.y-dan2))*8,createjs.Ease.quadInOut)
        .call(function(){
           this.flyDogMove();
        });
        
          
     };
     /*
     var lineg = new createjs.Graphics();
     var line = new createjs.Shape(lineg);
     lineg.setStrokeStyle(1);
     lineg.beginStroke("#f00");
     line.on('tick',function(){
        lineg.moveTo(flyDog.x,flyDog.y);
        lineg.lineTo(hero.x,hero.y);
        //console.log();
        
     });
     
     stage.addChild(line);
     */   
      flyDog.done = true;
      flyDog.on('tick',function(){
              
        var posi = hero.localToLocal(0,0,flyDog);
        var posi2 = hero.localToLocal(0,0,flyDog);
        if(flyDog.hitTest(posi.x+50,posi.y+50)||flyDog.hitTest(posi.x,posi.y)){
                if(flyDog.done){
                        overgame('你被天狗吃掉了！！');//+Math.sqrt(Math.pow(Math.abs(flyDog.x-hero.x),2)+Math.pow(Math.abs(flyDog.y-hero.y),2)));
               }
                
                flyDog.done = false;
        }
      });
      flyDog.on('added',function(){
         //月饼
        flyDog.flyDogMove()
      });
    stage.addChild(flyDog);
    
    /***以上是天狗代码***/
      
      //嫦娥
      /***以下嫦娥代码***/
      //var skyGirl = new createjs.Bitmap("img/change2.png");
      var skyGirlSheet = new createjs.SpriteSheet({
      	images:['./img/change.png'],
      	frames:{width:250,height:375,count:2},
      	animations:{
      		action:{
      			frames:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      			next:'normal',
      			speed:2
      		},
      		normal:1
      	}
      });
      var skyGirl = new createjs.Sprite(skyGirlSheet,'normal');
      skyGirl.scaleX = skyGirl.scaleY = stageW/240/5;
      skyGirl.x = 0;
      skyGirl.on('tick',(function(){
          var yp = 1;    
          return function(){
                yp+=0.06;
                skyGirl.y = Math.sin(yp)*30;
                if(skyGirl.x<-stageH/10||skyGirl.x>stageH-stageH/10)skyGirl.x = stageH/2;
          }    
      })());
      
     skyGirl.skyGirlMove = function(){
        var dan = Math.random()*stageW-20;     
        createjs.Tween.get(this,{override:true}).to({
                x:dan
        },Math.abs(this.x-dan*20),createjs.Ease.quadInOut)
        .call(function(){
           this.skyGirlMove();
           for(var i=0;i<3;i++){
                var _this = this;
                setTimeout(function(){
                        _this.initCake("",_this.x+20,skyGirl.y+20,Math.random()*8+1);        
                },i*150);
           }
        });
     }
     
      skyGirl.initCake = function(type,x,y,s){
      		  skyGirl.gotoAndPlay('action');	
              if(x<0&&x>stageW)return;
        			var candy = new createjs.Bitmap("img/mooncake("+parseInt(Math.random()*7+1)+").png");
        			stage.addChild(candy);
        			if(x>stageW-60)x=stageW-60;
        			else if(x<60)x=60;
        			candy.x = x||10;
        			candy.y = y||10;
        			var speed = s||2;
        			var candyWH = candy.getBounds();
        			candy.scaleX = candy.scaleY = stageW/240/10;
        			var random = Math.random();
        			createjs.Tween.get(candy).to({
        			     x:x+stageH*.15-random*stageH*.15,
        			     y:y+random*s-stageW*.1
        			},s*100,createjs.Ease.getPowOut(2)).to({
        			        y:stageH+stageW*.1
        			},s*500,createjs.Ease.getPowIn(2));
        			candy.on('tick',function(e){
        				var posiH = candy.localToLocal(0,0,hero);
        				var posiH2 = candy.localToLocal(-80,0,hero);
        				var posiH3 = candy.localToLocal(80,0,hero);
        				var posiF = candy.localToLocal(0,0,flyDog);
        				var posiF2 = candy.localToLocal(40,0,flyDog);
        				if(hero.hitTest(posiH.x,posiH.y)||hero.hitTest(posiH2.x,posiH2.y)||hero.hitTest(posiH3.x,posiH3.y)){
        				        stage.removeChild(candy);
        				        count.doMath();
        				}
        				if(candy.y>stageH||flyDog.hitTest(posiF.x,posiF.y)||flyDog.hitTest(posiF2.x,posiF2.y)){
        				        stage.removeChild(candy);
        				}
        			});     
	   }
      skyGirl.on('added',function(){
      	 //月饼
      	skyGirl.skyGirlMove()
      });
      stage.addChild(skyGirl);
      /***以上嫦娥代码***/
      

      /***以下后羿代码
     var landMan = new createjs.Bitmap("img/houyi.png");
     landMan.scaleX = landMan.scaleY = stageW/110/5;
     landMan.x = stageW/2;
     landMan.y = stageH-100;
     landMan.on('tick',function(){
             var posi = landMan.localToLocal(20,20,hero);
             if(landMan.hitTest(posi.x,posi.y)||landMan.hitTest(posi.x+30,posi.y)||landMan.hitTest(posi.x+50,posi.y)){
                    count.doneMath();            
             }
     });
     landMan.landManMove = function(){
        var dan = Math.random()*stageW-100;     
        createjs.Tween.get(this,{override:true}).to({
                x:dan
        },Math.abs(this.x-dan*20),createjs.Ease.elasticInOut)
        .call(function(){
           this.landManMove();
        });
     }
     landMan.on('added',function(){
                this.landManMove();     
     });
     
     stage.addChild(landMan);
     	以上后羿代码***/

    
        /***以下是计分系统***/
     var count = {
                num:0,
                numDom:document.getElementById('showInfo'),
                doMath:function(){
                        this.num++;
                        this.numDom.innerText = this.num;
                        if(this.num>0)this.showText.visible = true;
                        else return;
                        this.total += this.num;
                        this.showText.x = hero.x;
                        this.showText.y = hero.y;
                        this.showText.text = "+ "+this.num;
                        createjs.Tween.get(this.showText).to({y:hero.y-40},1500)
                        .call(function(){count.showText.visible = false;});
                        gameAudio(50);
                }
        };
        count.showText = new createjs.Text('+'+count.num,"24px Arial","#f00");
        stage.addChild(count.showText);
        count.showText.visible = false;
        
        /***以上是计分系统***/
      //循环帧事件
      createjs.Ticker.timingMode = createjs.Ticker.RAF;
      createjs.Ticker.setFPS(60);
      var startGame = function(){
              count.num = count.total = 0;
              createjs.Ticker.addEventListener('tick',function(e){
                      stage.update();
              });
              
              //结束
              setTimeout(function(){
                      overgame('游戏时间到！');
              },80000);
              function audioAutoPlay(id){  
                    var audio = document.getElementById(id);  
                    audio.play();  
                    document.addEventListener("WeixinJSBridgeReady", function () {  
                            audio.play();  
                    }, false);  
                    document.addEventListener('YixinJSBridgeReady', function() {  
                        audio.play();  
                    }, false);  
                }  
                audioAutoPlay('bgmusic');  
      };
        
      var overgame = function(txt){
        alert(txt);
        stage.canvas.style.display = 'none';
        //stage.removeAllChildren();
        var end = document.querySelector('.end');
        end.onclick=restart;
        end.style.display = "block";
        end.innerHTML = '<h1 style="color:#ff0074;text-align:center;margin-top:70%;width:60%;margin-left:20%;">你拿到了'+count.num+'个月饼</h1>';
        
      };
      var story = document.querySelector('.startBtn');
      story.onclick = function(){
              document.querySelector('.start').style.display = "none";
              startGame();
      };
      
  //})();
  var restart = function(){
        window.location.href=window.location.href+"?id="+10000*Math.random();
}